package com.xiaogao.autofill.aspect;

import com.xiaogao.common.entity.BaseResponse;
import com.xiaogao.common.entity.PageData;
import com.xiaogao.sys.service.SysUserService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Aspect
@Component
public class FillUserInfoAspect {

    @Autowired
    private SysUserService sysUserService;

    @AfterReturning(pointcut = "@annotation(com.xiaogao.autofill.annotation.FillUserInfo)", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) {
        if (result instanceof PageData) { // PageData
            PageData<?> pageData = (PageData<?>) result;
            processList(pageData.getData()); // 假设getRecords()返回List
        } else if (result instanceof List) { // List
            processList((List<?>) result);
        }
    }

    private void processList(List<?> list) {
        if (list != null && !list.isEmpty() && list.get(0) instanceof BaseResponse) {
            sysUserService.fillUserInfo((List<BaseResponse>) list);
        }
    }
}
